export function request(url, data) {
	uni.showLoading({
		title: '加载中',
		mask: true
	})
	if (url == 'getPhone') {
		return new Promise((resolve, reject) => {
			wx.cloud.callFunction({
				name: "getPhone",
				data: {
					...data
				},
				success: (res) => {
					if (res?.result?.errCode == 0) {
						resolve({
							result: res.result.list[0] ?? null,
							status: true,
						})
					} else {
						reject({
							result: '获取失败',
							status: false,
						})
					}

				},
				error: (res) => {
					reject({
						result: res.errMsg,
						status: false,
					})
				},
				complete: (res) => {
					uni.hideLoading()
				},
			});
		})

	} else if (url == 'getOpenId') {
		return new Promise((resolve, reject) => {
			wx.cloud.callFunction({
				name: "quickstartFunctions",
				data: {
					type: url,
				},
				success: (res) => {
					// console.log('success', res)
					if (res?.result?.openid) {
						resolve({
							result: res.result.openid,
							status: true,
						})
					} else {
						reject({
							result: '获取失败',
							status: false,
						})
					}

				},
				error: (res) => {
					// console.log('error', res)
					reject({
						result: res.errMsg,
						status: false,
					})
				},
				complete: (res) => {
					// console.log('complete', res);
					uni.hideLoading()
				},
			});
		})
	} else {
		let userId = uni.getStorageSync("userid")
		if (!userId) {
			uni.showToast({
				title: "暂未登录，请先登录",
				icon: "none"
			})
			uni.navigateTo({
				url: "/pages/index/index"
			})
		}
		return new Promise((resolve, reject) => {
			wx.cloud.callFunction({
				name: "quickstartFunctions",
				data: {
					type: url,
					data
				},
				success: (res) => {
					// console.log('success', res)
					if (res?.result?.success) {
						resolve({
							result: res.result.data,
							status: true,
						})
					} else {
						console.log('请求失败')
						resolve({
							result: res.result,
							status: false,
						})
					}

				},
				error: (res) => {
					reject({
						result: res.errMsg,
						status: false,
					})
				},
				complete: (res) => {
					uni.hideLoading()
				},
			});
		})
	}


}

export function formatDateTime(timestamp) {
	console.log('timestamp', timestamp)
	// 格式化时间戳为显示文本
	if (!timestamp) return ''
	const date = new Date(timestamp)
	const year = date.getFullYear()
	const month = (date.getMonth() + 1).toString().padStart(2, '0')
	const day = date.getDate().toString().padStart(2, '0')
	const hour = date.getHours().toString().padStart(2, '0')
	const minute = date.getMinutes().toString().padStart(2, '0')
	// return `${year}-${month}-${day} ${hour}:${minute}`
	return `${month}/${day} ${hour}:${minute}`

};

export function getOptionLabel(value, list) {
	// console.log('value, list :>> ', value, list);
	let label = list.find((item) => item.value == value).label;
	// console.log('label :>> ', label);
	return label;
}